﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TP_02.Models;

namespace TP_02.Controllers
{
    public class GameCategoryController : Controller
    {
        // GET: GameCategory
        public ActionResult Index()
        {
            return View(GameCategory.GetList());
        }

        // GET: GameCategory/Details/5
        public ActionResult Details(int id)
        {
            return View(GameCategory.FindOne(id));
        }

        // GET: GameCategory/Create
        [Authorize]
        public ActionResult Create()
        {
            return View(new GameCategory());
        }

        // POST: GameCategory/Create
        [HttpPost]
        [Authorize]
        public ActionResult Create(GameCategory gameCategory)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return View(gameCategory);
                }

                gameCategory.SaveAsNew();

                return RedirectToAction("Index");
            }
            catch
            {
                return View(gameCategory);
            }
        }

        // GET: GameCategory/Edit/5
        [Authorize]
        public ActionResult Edit(int id)
        {
            return View(GameCategory.FindOne(id));
        }

        // POST: GameCategory/Edit/5
        [HttpPost]
        [Authorize]
        public ActionResult Edit(int id, GameCategory gameCategory)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return View(gameCategory);
                }

                gameCategory.Update();

                return RedirectToAction("Index");
            }
            catch
            {
                return View(gameCategory);
            }
        }

        // GET: GameCategory/Delete/5
        [Authorize]
        public ActionResult Delete(int id)
        {
            return View(GameCategory.FindOne(id));
        }

        // POST: GameCategory/Delete/5
        [HttpPost]
        [Authorize]
        public ActionResult Delete(int id, GameCategory gameCategory)
        {
            try
            {
                GameCategory.Destroy(id);

                return RedirectToAction("Index");
            }
            catch
            {
                return View(gameCategory);
            }
        }
    }
}
